﻿using System;
using System.Windows;
using System.Windows.Controls;
using jSLApp.Client.MainService;
using jSLCore.Client.Presentation.BaseControl;

namespace jSLApp.Client.Forms
{
    /// <summary>
    /// Comment Creation
    /// </summary>
    public partial class NewComment : DialogControl
    {
        private readonly int? _commentableId;

        public NewComment(int commentableId):this()
        {
            _commentableId = commentableId;
        }

        /// <summary>
        /// only for designers use
        /// </summary>
        public NewComment()
        {
            InitializeComponent();
        }

        public bool IsRatingEnabled { get { return ratePanel.Visibility ==Visibility.Visible; } set
        {
            ratePanel.Visibility=value?Visibility.Visible : Visibility.Collapsed;
           
        } }

        public string Quote { get; set; }
        public string WhoAndWhenQuote { get; set; }

        public override void LoadInitialData()
        {   
            if(!string.IsNullOrEmpty(WhoAndWhenQuote))
            {
                txt.InsertReplyText(WhoAndWhenQuote,Quote);
            }

            
            if (IsRatingEnabled)
            {//Check if rating is enabled
                DoWork("Загрузка...",(sender, args) =>
                                         {
                                             args.Result = Communication.Proxy.CanRate(args.Int);
                                         },(o, eventArgs) =>
                                                               {
                                                                   if (eventArgs.Error == null)
                                                                   {
                                                                       if (!(bool) eventArgs.Result)
                                                                       {
                                                                           IsRatingEnabled = false;
                                                                       }
                                                                       else
                                                                       {
                                                                           ratePanel.DataContext = new Rate();
                                                                           Bind();
                                                                       }
                                                                   }
                                                               },_commentableId);
            }

           
        }

        public Rate Rate
        {
            get { return (Rate) ratePanel.DataContext; }
        }

        public override void OkClicked()
        {
            Rate rate = null;
            if(IsRatingEnabled)
            {
                if(danceRate.Value!=null || musicRate.Value!=null || qualityRate.Value!=null)
                {
                    rate = (Rate) ratePanel.DataContext;
                }
            }
            if (_commentableId != null)
            {
                DoWork("Добавление комментария", (sender, args) => Communication.Proxy.CreateComment(args.Int,
                                                                                                     (string) args.Params[1],
                                                                                                     (Rate) args.Params[2]), (o, eventArgs) =>
                                                            {
                                                                if (eventArgs.Error == null)
                                                                {
                                                                    InvokeDataSelected();
                                                                }
                                                            }, _commentableId, txt.jSLGetText(), rate);
            }
            else
            {
                var str = txt.jSLGetText();
                InvokeDataSelected(str);
            }
        }

        public override bool IsOkVisible
        {
            get { return true; }
        }

        public override bool IsStandartOkCancelEnabled
        {
            get { return true; }
        }

        public override object OkContent
        {
            get { return "Комментировать"; }
        }

        public override bool AutoSize
        {
            get { return true; }
        }

        public override Control GetControlForInitialFocus()
        {
            return txt;
        }

        private void nullable_Clicked(object sender, EventArgs e)
        {
            var element = (FrameworkElement)sender;
            switch(element.Tag.ToString())
            {
                case "music":
                    Rate.RateMusic = null;
                    break;
                case "dance":
                    Rate.RateDance = null;
                    break;
                case "quality":
                    Rate.RateQuality = null;
                    break;
            }
        }
    }
}
